﻿namespace egret3d
{
    export class ParticleShape
    {
        private _vertexCount: number;

        public get vertexCount(): number
        {
            return this._vertexCount;
        }

        public set vertexCount(value: number)
        {
            if (this._vertexCount == value)
            {
                return;
            }
            var dataCount: number = value * this.vertexAttLength;

            var data: Float32Array = null;
            if (this.vertexArray)
            {
                if (this.vertexCount < value)
                {
                    data = new Float32Array(dataCount);
                    data.set(this.vertexArray);
                    delete this.vertexArray;
                }
                else
                {
                    data = this.vertexArray;
                }
            }
            else
            {
                data = new Float32Array(dataCount);
            }
            this.vertexArray = data;
            this._vertexCount = value;
        }

        _indexCount: number;
        private _totalIndexCount: number = 0;
        public get indexCount(): number
        {
            return this._indexCount;
        }

        public set indexCount(value: number)
        {
            this._indexCount = value;

            if (this._totalIndexCount >= value)
            {
                return;
            }

            var data: Uint16Array = new Uint16Array(value);
            if (this.indexArray)
            {
                data.set(this.indexArray);
                delete this.indexArray;
            }
            this.indexArray = data;

            this._totalIndexCount = value;
        }

        vertexArray: Float32Array;
        indexArray: Uint16Array;

        vertexAttLength: number;

        constructor()
        {
            //定义顶点数据长度
            this.vertexAttLength = 15;
        }

        public getVertexForIndex(index: number)
        {
            var target: Array<number> = new Array<number>();
            if (index < 0 || index >= this.vertexCount)
            {
                return target;
            }

            for (var i = 0; i < this.vertexAttLength; i++)
            {
                target.push(this.vertexArray[index * this.vertexAttLength + i]);
            }
            return target;
        }
    }
}